<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Donors extends CI_Controller {

 function __construct() {
   parent::__construct();
   $this->load->model('donors_model','',TRUE);
 }

 //Loads the users view
 function index() {
   if($this->session->userdata('logged_in')) {
     $session_data = $this->session->userdata('logged_in');
     $data['ds_usuario'] = $session_data['ds_usuario'];
     $this->load->view('donors_view', $data);
   } else {
     redirect('login', 'refresh');
   }
 }

function savedonor() {
  if($this->input->post("donorname") 
    && $this->input->post("donortel") && $this->input->post("donormail") 
    && $this->input->post("donordir") && $this->input->post("donorcountry")){
      $donorid = $this->input->post('donorid');
      $donorname = $this->input->post('donorname');
      $donortel = $this->input->post('donortel');
      $donormail = $this->input->post('donormail');
      $donorcountry = $this->input->post('donorcountry'); 
      $donordir = $this->input->post('donordir');
      //query the database
      $result = $this->donors_model->save($donorid, $donorname, $donortel, $donormail, $donorcountry, $donordir);
      if($result) {
        echo json_encode(array("response" => "success"));
      } else {
        echo json_encode(array("response" => "failed"));
      }
  } else {
      echo json_encode(array("response" => "incomplete_form"));
  }

 }

 function returnalldonors() {
   //query the database
   $result = $this->donors_model->getalldonors();
   if($result) {
     echo json_encode($result);
   }
 }

 function returndonorbyid($id) {
   //query the database
   $result = $this->donors_model->getdonorbyid($id);
   if($result) {
     echo json_encode($result);
   }
 }
}

?>